﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using visinia.SmartEngine.Installer;

namespace visinia.web.admin.pages
{
    public partial class InstallerZips : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List<string> zips = InstallerLocalContentController.GetLocalZipFiles();
            List<ZipData> dataList = new List<ZipData>();
            foreach (string zip in zips)
            {
                if (!dataList.Exists(p => p.Zip == zip))
                {
                    ZipData data = new ZipData();
                    data.Zip = zip;
                    data.contents = InstallerLocalContentController.GetLocalZipFilesContents().FindAll(p => p.ZipFileName == zip);
                    dataList.Add(data);
                }
            }
            ZipList.DataSource = dataList;
            ZipList.DataBind();
        }

        public class ZipData
        {
            private string _Zip;
            public string Zip { get { return _Zip; } set { _Zip = value; } }
            public List<ContentFileInfo> contents = new List<ContentFileInfo>();
        }

        protected void ZipList_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            Repeater ContentList = (Repeater)e.Item.FindControl("ContentList");
            if (ContentList != null)
            {
                ZipData data = (ZipData)e.Item.DataItem;
                ContentList.DataSource = data.contents;
                ContentList.DataBind();
            }
        }

        protected void ZipList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "DeleteZip")
            {
                string zipFileName = (string)e.CommandArgument;
                Message.InnerText = InstallController.RemoveInstallerZip(zipFileName);
                Message.Visible = true;
            }
        }
    }
}